from django.http import HttpResponse
from django.shortcuts import render
from rest_framework.response import Response

from rest_framework.views import APIView
# Create your views here.
from users.models import User

# /users/username/(?P<username>\w{5,20})/count/
from users.serializers import RegisterUser


class UserName(APIView):

    def get(self,request,username):

        count = User.objects.filter(username=username).count()
        context={
            'count':count,
            'username':username,
        }

        return Response(context)

# /users/usermobile/(?P<usermobile>1[3-9]\d{9})/count/
class UserMobile(APIView):

    def get(self,request,mobile):

        count = User.objects.filter(mobile=mobile).count()
        context = {
            'count': count,
            'username': mobile,
        }
        return Response(context)

class Register(APIView):

    def post(self,request):
        data = request.data
        register = RegisterUser(data=data)
        register.is_valid(raise_exception=True)
        register.save()
        return Response(register.data)